#include "pch.h"

using std::exception;

Board::Board(int size) {
	this->size = size;
	this->emptyy = size * size;
	memset(this->data, 0, sizeof(this->data));
}

Board::Board(const Board & board) {
	this->size = board.size;
	this->emptyy = board.emptyy;
	memcpy(this->data, board.data, sizeof(board.data));
}

int Board::get(int pos) const {
	return get(pos / size, pos % size);
}

int Board::get(int x, int y) const {
	if (x < 0 || x >= this->size) {
		return RED;
	}
	if (y < 0 || y >= this->size)
	{
		return BLUE;
	}

	return data[x][y];
}

void Board::set(int x, int y, int val) {
	if (x < 0 || x >= this->size || y < 0 || y >= this->size) {
		throw exception("set board out of range\n");
	}

	if (data[x][y] != EMPTY && val == EMPTY) {
		this->emptyy++;
	}
	if (data[x][y] == EMPTY && val != EMPTY) {
		this->emptyy--;
	}

	data[x][y] = val;
}

void Board::set(int pos, int val) {
	set(pos / size, pos % size, val);
}

int Board::s() const {
	return size;
}

int Board::empty() const {
	return this->emptyy;
}